
package com.gmail.dengtao.joe.game.server.config;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Node;

import com.gmail.dengtao.joe.commons.util.NumberUtils;
import com.gmail.dengtao.joe.commons.util.StringUtils;
import com.gmail.dengtao.joe.game.server.exception.AcceptorConfigInvalidException;
import com.gmail.dengtao.joe.game.server.exception.GroupConfigInvalidException;
import com.gmail.dengtao.joe.group.member.Member;

public class GroupConfig {

	private String name;
	private String host;
	private int port;
	private int receiveBufSize = 512;
	private int sendBufSize = 512;
	
	private List<Member> members = new ArrayList<Member>();
	
	public GroupConfig(Document	doc) throws Exception {
		Node group = doc.selectSingleNode("//server/group");
		if (group == null) throw new AcceptorConfigInvalidException();
		String tmp = group.valueOf("@name");
		if (StringUtils.isBlank(tmp)) throw new GroupConfigInvalidException("invalid name!");
		else this.name = tmp;
		
		tmp = group.valueOf("@host");
		if (StringUtils.isBlank(tmp)) throw new GroupConfigInvalidException("invalid host!");
		else this.host = tmp;

		tmp = group.valueOf("@port");
		if (NumberUtils.isInt(tmp) && NumberUtils.isPositive(tmp)) {
			this.port = Integer.parseInt(tmp);
		} else {
			throw new AcceptorConfigInvalidException("invalid host!");
		}
		
		tmp = group.valueOf("@receive_buf_size");
		if (NumberUtils.isInt(tmp) && NumberUtils.isPositive(tmp)) {
			this.receiveBufSize = Integer.parseInt(tmp);
		}
		
		tmp = group.valueOf("@send_buf_size");
		if (NumberUtils.isInt(tmp) && NumberUtils.isPositive(tmp)) {
			this.sendBufSize = Integer.parseInt(tmp);
		}
		
		@SuppressWarnings("unchecked")
		List<Node> members = doc.selectNodes("//server/group/member");
		for (Node member : members) {
			this.members.add(new Member(member.valueOf("@host"), Integer.parseInt(member.valueOf("@port"))));
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public int getReceiveBufSize() {
		return receiveBufSize;
	}

	public void setReceiveBufSize(int receiveBufSize) {
		this.receiveBufSize = receiveBufSize;
	}

	public int getSendBufSize() {
		return sendBufSize;
	}

	public void setSendBufSize(int sendBufSize) {
		this.sendBufSize = sendBufSize;
	}

	public List<Member> getMembers() {
		return members;
	}

	public void setMembers(List<Member> members) {
		this.members = members;
	}

	@Override
	public String toString() {
		return "GroupConfig [name=" + name + ", host=" + host + ", port="
				+ port + ", receiveBufSize=" + receiveBufSize
				+ ", sendBufSize=" + sendBufSize + ", members=" + members + "]";
	}

}